package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ILegalArticleService;
import com.mdd.admin.validate.LegalArticleCreateValidate;
import com.mdd.admin.validate.LegalArticleSearchValidate;
import com.mdd.admin.validate.LegalArticleUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalArticleDetailVo;
import com.mdd.admin.vo.LegalArticleListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalArticle;
import com.mdd.common.mapper.LegalArticleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 文章公告实现类
 * @author LikeAdmin
 */
@Service
public class LegalArticleServiceImpl implements ILegalArticleService {
        
    @Resource
    LegalArticleMapper legalArticleMapper;

    /**
     * 文章公告列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalArticleListedVo>
     */
    @Override
    public PageResult<LegalArticleListedVo> list(PageValidate pageValidate, LegalArticleSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LegalArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("deleted_at");
//        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        legalArticleMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:content:str",
            "=:type:int",
            "=:mainPic@main_pic:str",
            "=:mainPicThumb@main_pic_thumb:str",
            "=:deletedAt@deleted_at:str",
            "=:createdBy@created_by:long",
            "=:createdAt@created_at:str",
            "=:isShow@is_show:int",
            "=:sort:long",
            "=:desc:str",
        });

        IPage<LegalArticle> iPage = legalArticleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LegalArticleListedVo> list = new LinkedList<>();
        for(LegalArticle item : iPage.getRecords()) {
            LegalArticleListedVo vo = new LegalArticleListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 文章公告详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalArticle
     */
    @Override
    public LegalArticleDetailVo detail(Integer id) {
        LegalArticle model = legalArticleMapper.selectOne(
                new QueryWrapper<LegalArticle>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalArticleDetailVo vo = new LegalArticleDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 文章公告新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalArticleCreateValidate createValidate) {
        LegalArticle model = new LegalArticle();
        model.setTitle(createValidate.getTitle());
        model.setContent(createValidate.getContent());
        model.setType(createValidate.getType());
        model.setMainPic(createValidate.getMainPic());
        model.setMainPicThumb(createValidate.getMainPicThumb());
        model.setDeletedAt(createValidate.getDeletedAt());
        model.setCreatedBy(createValidate.getCreatedBy());
        model.setCreatedAt(createValidate.getCreatedAt());
        model.setIsShow(createValidate.getIsShow());
        model.setSort(createValidate.getSort());
        model.setDesc(createValidate.getDesc());
        legalArticleMapper.insert(model);
    }

    /**
     * 文章公告编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalArticleUpdateValidate updateValidate) {
        LegalArticle model = legalArticleMapper.selectOne(
                new QueryWrapper<LegalArticle>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setContent(updateValidate.getContent());
        model.setType(updateValidate.getType());
        model.setMainPic(updateValidate.getMainPic());
        model.setMainPicThumb(updateValidate.getMainPicThumb());
        model.setDeletedAt(updateValidate.getDeletedAt());
        model.setCreatedBy(updateValidate.getCreatedBy());
        model.setCreatedAt(updateValidate.getCreatedAt());
        model.setIsShow(updateValidate.getIsShow());
        model.setSort(updateValidate.getSort());
        model.setDesc(updateValidate.getDesc());
        legalArticleMapper.updateById(model);
    }

    /**
     * 文章公告删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalArticle model = legalArticleMapper.selectOne(
                new QueryWrapper<LegalArticle>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalArticleMapper.delete(new QueryWrapper<LegalArticle>().eq("id", id));
    }

}
